package com.course.server;

import com.course.bean.User;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@Tag(name = "post请求")
@RequestMapping("v1")
public class PostMethod {

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @Operation(summary = "登录", description = "登录接口获得cookies信息")
    public String login(HttpServletResponse response,
                        @RequestParam(value = "username", required = true) String userName,
                        @RequestParam(value = "password") String pwd) {
        if (userName.equals("star") && pwd.equals("123456")) {
            Cookie cookie = new Cookie("login", "true");
            response.addCookie(cookie);

            return "登录成功";
        }

        return "用户名后者密码错误";
    }

    @RequestMapping(value = "/getUser", method = RequestMethod.POST)
    @Operation(summary = "获取用户", description = "获取用户信息")
    public String getUser(HttpServletRequest request, @RequestBody User user) {
        Cookie[] cookies = request.getCookies();

        for (Cookie c : cookies) {
            if (c.getName().equals("login") && c.getValue().equals("true")
                    && user.getName().equals("star") && user.getPassword().equals("123456")) {
                User user1 = new User();
                user1.setName("Ray");
                user1.setAge(18);

                return user1.toString();
            }
        }

        return "参数不合法";
    }
}
